/*
 * 文 件    名:  AdPromotionModel.java
 * 创建日期:  2012-10-29上午11:04:14
 * 版          权:  Epro Technologies Co., Ltd. Copyright 2011-2012,  All rights reserved
 * 作          者:  tj072
 */
package com.weipu.common.facade.model;

import java.io.Serializable;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/** 
 * @Description  广告促销
 * @version [版本号, 2012-10-29]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 * 
 * @author tj072
 */
public class AdPromotionModel implements Serializable
{
    /** 
     * @Fields serialVersionUID : 序列版本号
     */
    private static final long serialVersionUID = -5795393022266261884L;
    
    /**
     * 广告id
     */
    private int id;
    
    /**
     * 广告图片地址
     */
    private String imageUrl;
    
    @Override
    public String toString()
    {
        return tojson().toString();
    }
    
    public int getId()
    {
        return id;
    }
    
    public void setId(int id)
    {
        this.id = id;
    }
    
    public String getImageUrl()
    {
        return imageUrl;
    }
    
    public void setImageUrl(String imageUrl)
    {
        this.imageUrl = imageUrl;
    }
    
    public JSONObject tojson()
    {
        JSONObject json = new JSONObject();
        
        try
        {
            if (-1 != id)
            {
                json.put("id", id);
            }
            
            if (null != imageUrl)
            {
                json.put("ImageUrl", imageUrl);
            }
            
        }
        catch (JSONException e)
        {
        }
        
        return json;
    }
    
    /**
      * JSON对象转存到广告model
      * @param json
      * @return  AdPromotionModel对象
      * @return AdPromotionModel 
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
     */
    public static AdPromotionModel json2AdPromotionModel(JSONObject json)
    {
        AdPromotionModel model = new AdPromotionModel();
        if (null != json)
        {
            model.setId(json.optInt("id"));
            model.setImageUrl(json.optString("ImageUrl"));
        }
        
        return model;
    }
    
    /**
      * JSON字符串转到广告model
      * @param jsonString
      * @return  AdPromotionModel对象
      * @return AdPromotionModel [返回类型说明]
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
     */
    public static AdPromotionModel json2AdPromotionModel(String jsonString)
    {
        if (null == jsonString || "".equals(jsonString.trim()))
        {
            return null;
        }
        
        AdPromotionModel model = null;
        try
        {
            JSONObject json = new JSONObject(jsonString);
            model = json2AdPromotionModel(json);
        }
        catch (JSONException e)
        {
        }
        return model;
    }
    
    /**  
      *  JSON数组转存到数组列表
      * @param arrays
      * @return  AdPromotionModel对象的数组
      * @return ArrayList<AdPromotionModel> 
      * @exception throws [违例类型] [违例说明]
      * @see [类、类#方法、类#成员]
     */
    public static ArrayList<AdPromotionModel> jsonArray2AdPromotionModel(JSONArray arrays)
    {
        ArrayList<AdPromotionModel> AdPromotionModelList = null;
        if (null != arrays && arrays.length() > 0)
        {
            AdPromotionModelList = new ArrayList<AdPromotionModel>(arrays.length());
            for (int i = 0; i < arrays.length(); i++)
            {
                AdPromotionModelList.add(AdPromotionModel.json2AdPromotionModel(arrays.optJSONObject(i)));
            }
        }
        return AdPromotionModelList;
    }
}
